<template>
  <div class="app-container">
   
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="请输入任务名称" v-model="listQuery.jobName"></el-input>
      <el-button class="filter-item" type="primary" style="float:right;"  icon="el-icon-edit"  @click="handleCreate">新增</el-button>
      <el-button class="filter-item" type="primary" style="float:right;margin-right:10px;" v-waves icon="el-icon-search" @click="handleFilter">查询</el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="正在加载中" border fit highlight-current-row
      style="width: 100%">
      <el-table-column align="center" label="行号" width="65">
        <template slot-scope="scope">
          <span>{{scope.$index+1}}</span>
        </template>
      </el-table-column>
      <el-table-column width="150px" align="center" label="任务名称">
        <template slot-scope="scope">
          <span>{{scope.row.jobName }}</span>
        </template>
      </el-table-column>
      <el-table-column width="150px" align="center" label="任务分组">
        <template slot-scope="scope">
          <span>{{scope.row.jobGroup }}</span>
        </template>
      </el-table-column>
      <el-table-column width="200px" align="center" label="执行时间">
        <template slot-scope="scope">
          <span>{{scope.row.cronExpression}}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="200px" align="center" label="描述">
        <template slot-scope="scope">
          <span class="link-type" >{{scope.row.description}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
         <el-button type="primary" size="mini" @click="handleTrrigger(scope.row)">执行</el-button>
         <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
         <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
         <el-button type="success" v-if="scope.row.triggerState =='PAUSED'" size="mini" @click="handleResume(scope.row)">恢复</el-button>
         <el-button type="success" v-else size="mini" @click="handlePause(scope.row)">停止</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="90px" style='width: 450px; margin-left:50px;'>
        <el-input type="hidden" v-model="temp.appId"></el-input>
        <el-form-item label="任务名称" prop="jobName">
          <el-input v-model="temp.jobName" v-if="dialogStatus=='create'" placeholder="请输入任务名称"></el-input>
          <el-input v-model="temp.jobName" v-else placeholder="请输入任务名称" readonly></el-input>
        </el-form-item>
        <el-form-item label="任务分组" prop="jobGroup">
          <el-input v-model="temp.jobGroup" placeholder="请输入任务分组"></el-input>
        </el-form-item>
        <el-form-item label="执行类" prop="jobClassName">
          <el-input v-model="temp.jobClassName" placeholder="请输入执行类"></el-input>
        </el-form-item>
        <el-form-item label="执行时间" prop="cronExpression">
          <el-input v-model="temp.cronExpression" placeholder="请输入执行时间"></el-input>
        </el-form-item>
        <el-form-item label="description" prop="description">
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入描述" v-model="temp.description">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">cancel</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">保存</el-button>
        <el-button v-else type="primary" @click="updateData">修改</el-button>
      </div>
    </el-dialog>

    <el-dialog title="Reading statistics" :visible.sync="dialogPvVisible">
      <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
        <el-table-column prop="key" label="Channel"> </el-table-column>
        <el-table-column prop="pv" label="Pv"> </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">confirm</el-button>
      </span>
    </el-dialog>
    

  </div>
</template>

<script>
import { getList, addOrUpdate, trigger, pause, remove, resume } from '@/api/quartz'
import waves from '@/directive/waves' // 水波纹指令

export default {
  name: 'quartz',
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        jobName: '',
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      temp: {
        "jobName":"",//任务名称
	      "jobGroup":"",//任务分组
	      "description":"",//任务描述
	      "jobClassName":"",//执行类
	      "cronExpression":"",//执行时间
	      "triggerName":"",//执行时间
	      "triggerState":"",//任务状态
	      "oldJobName":"",//任务名称 用于修改
	      "oldJobGroup":""//任务分组 用于修改
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        jobName: [{ required: true, message: 'jobName is required', trigger: 'blur' }],
        jobGroup: [{ required: true, message: 'jobGroup is required', trigger: 'change' }],
        jobClassName: [{ required: true, message: 'jobClassName is required', trigger: 'change' }],
        cronExpression: [{ required: true, message: 'cronExpression is required', trigger: 'change' }]
      }
    }
  },
  filters: {
    formatDate(time) {
       var date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm');
    }   
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      const param = {
        'body': {
          "quartzDTO":{
            "jobName":this.listQuery.jobName
          },
          "pageInfo":{
            "page":this.listQuery.page,
            "pageSize":this.listQuery.limit
          }
        }
      }
      getList(param).then(response => {
        this.list = response.body.quartzDTOList
        this.total = response.body.pageInfo.rowCount
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    resetTemp() {
      this.temp = {
        "jobName":undefined,
	      "jobGroup":undefined,
	      "description":undefined,
	      "jobClassName":undefined,
	      "cronExpression":undefined,
	      "triggerName":undefined,
	      "triggerState":undefined,
	      "oldJobName":undefined,
	      "oldJobGroup":undefined
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const data = {
            'body': {
              "quartzDTO":{
                "jobName":this.temp.jobName,
                "jobGroup": this.temp.jobGroup,
                "description": this.temp.description,
                "jobClassName": this.temp.jobClassName,
                "cronExpression": this.temp.cronExpression
              }
            }
          }
          addOrUpdate(data).then(response => {
            this.list.unshift(response.body.quartzDTO)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.oldJobName = this.temp.jobName
      this.temp.oldJobGroup = this.temp.jobGroup
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          // const tempData = Object.assign({}, this.temp)
          const data = {
            'body': {
              "quartzDTO":{
                "jobName":this.temp.jobName,
                "jobGroup": this.temp.jobGroup,
                "description": this.temp.description,
                "jobClassName": this.temp.jobClassName,
                "cronExpression": this.temp.cronExpression,
                "oldJobName":this.temp.oldJobName,
			          "oldJobGroup":this.temp.oldJobGroup
              }
            }
          }
          addOrUpdate(data).then(response => {
            const quartzDTO = response.body.quartzDTO;
            for (const v of this.list) {
              if (v.jobName === this.temp.jobName) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, quartzDTO)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(row) {
      
      const data = {
          'body': {
             "quartzDTO":{
                "jobName":row.jobName,
                "jobGroup": row.jobGroup
              }
            }
          }
          remove(data).then(response => {
            const quartzDTO = response.body.quartzDTO;
            for (const v of this.list) {
              if (v.jobName === row.jobName) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, quartzDTO)
                break
              }
            }
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
      const index = this.list.indexOf(row)
      this.list.splice(index, 1)
    },
    handleTrrigger(row){
        const data = {
          'body': {
             "quartzDTO":{
                "jobName":row.jobName,
                "jobGroup": row.jobGroup
              }
            }
          }
          trigger(data).then(response => {
            const quartzDTO = response.body.quartzDTO;
            this.$notify({
              title: '成功',
              message: '执行成功',
              type: 'success',
              duration: 2000
            })
          })
    },
    handlePause(row){
        const data = {
          'body': {
             "quartzDTO":{
                "jobName":row.jobName,
                "jobGroup": row.jobGroup
              }
            }
          }
          pause(data).then(response => {
            const quartzDTO = response.body.quartzDTO;
            this.$notify({
              title: '成功',
              message: '停止成功',
              type: 'success',
              duration: 2000
            })
          })
    },
    handleResume(row){
        const data = {
          'body': {
             "quartzDTO":{
                "jobName":row.jobName,
                "jobGroup": row.jobGroup
              }
            }
          }
          resume(data).then(response => {
            const quartzDTO = response.body.quartzDTO;
            this.$notify({
              title: '成功',
              message: '恢复成功',
              type: 'success',
              duration: 2000
            })
          })
    },
  }
}
</script>
